package com.jams.goodox.activity.serve;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;

import com.github.lzyzsd.library.BridgeWebView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.common.SimpleHtmlActivity;
import com.jams.goodox.activity.home.ServiceGo;
import com.jams.goodox.entity.RepairFocusBean;
import com.jams.goodox.entity.ServerBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.utils.WebViewUtils;
import com.jams.library.utils.UniversalDeviceUtils;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 焦点新闻
 * Created by jams_zhen on 2016/9/14.
 */
public class FocusNewActivity extends BaseActivity implements BridgeWebView.LoadListener {

    @Bind(R.id.webView)
    BridgeWebView webView;
    @Bind(R.id.btn_go)
    Button mButton;
    @Bind(R.id.img_bottom)
    ImageView img_bottom;
    @Bind(R.id.img_bottom_out)
    ImageView img_bottom_out;


    private RepairFocusBean.DataBean dataBean;
  

    private ServerBean merchantListBean;

    @Override
    protected int getLayoutId() {
        return R.layout.repair_focus_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "详情");//改成详情
        String id = getIntent().getStringExtra("id");
        getDate(id);
    }


    @OnClick(R.id.btn_go)
    public void onClick() {
        Intent in;
        if (dataBean == null)
            return;
        switch (dataBean.getSkipType()) {
            case "1"://服务
                ItemClickListener();
                break;
            case "2"://商品
                break;
            case "3"://链接
                in = new Intent(this, SimpleHtmlActivity.class);
                in.putExtra(SimpleHtmlActivity.URL, dataBean.getUrl());
                startActivity(in);
                break;
            default:
                break;

        }
    }

    /**
     * 获取焦点新闻详情
     */
    private void getDate(String id) {
        showLoading();
        enqueue(createCallService(ServiceService.class).getFocus(id), new RetrofitCallbackListener<RepairFocusBean>() {
            @Override
            public void onSuccess(Call<RepairFocusBean> call, RepairFocusBean repairFocusBean) {
                //misLoading();
                dataBean = repairFocusBean.getData();
                if (dataBean == null)
                    return;
                if ("0".equals(repairFocusBean.getData().getSkipType())) {
                    mButton.setVisibility(View.GONE);
                } else {
                    mButton.setVisibility(View.VISIBLE);
                }
                merchantListBean = dataBean.getSubject();
                setDate();
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    /**
     * 设置界面数据
     */
    private void setDate() {
        String title = dataBean.getTitle();
        String time = dataBean.getIntime();
        String childTitle = SePref.getCommunityName();
        String redNums = dataBean.getReadnum()+"";
        String content = dataBean.getContent();

        webView.setLoadListener(this);
        WebViewUtils.setCustomDataScriptListener(webView, title + "", time + "", childTitle + "", "阅读 " + redNums, content + "");
    }

    /**
     * 进入不同的服务
     */
    private void ItemClickListener() {
        if (merchantListBean == null)
            return;
        ServiceGo.gotoProductPage(this, merchantListBean);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        showLoading();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //获取加载完成后的h5页面高度，延迟避免部分手机获取不到webview高度
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                int height = webView.getHeight();
                if (UniversalDeviceUtils.getDisplayHeight(FocusNewActivity.this) > height + Utils.dp2px(FocusNewActivity.this, 190)) {
                    img_bottom_out.setVisibility(View.VISIBLE);
                    img_bottom.setVisibility(View.GONE);
                } else {
                    img_bottom_out.setVisibility(View.GONE);
                    img_bottom.setVisibility(View.VISIBLE);
                }
                misLoading();
            }});
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

    }
}
